package com.example.demo.service;

import com.example.demo.dao.UserRepository;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;


@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public Boolean addUser(User user) {
        Boolean falg = true;
        try {
            userRepository.save(user);
        } catch (Exception e) {
            e.printStackTrace();
            falg = false;
        }
        return falg;
    }

    public Boolean deleteUser(Integer id) {
        Boolean falg = true;
        User user = userRepository.findById(id).get();
        try {
            userRepository.delete(user);
        } catch (Exception e) {
            e.printStackTrace();
            falg = false;
        }
        return falg;
    }

    public User selectUser(Integer id) {
        return userRepository.findById(id).get();
    }

    public User findByUserName(String name) {
        return userRepository.findByUsername(name);
    }

    public User findUserBySex(String sex) {

        return userRepository.findUserBySex(sex);
    }
}
